#ifndef RESOURCES_H
#define RESOURCES_H

#include <string>
#include <iostream>
#include <dirent.h>
#include <unistd.h>
#include <tr1/memory>
#include <list>
#include <set>

#include <sys/stat.h>

#include "core/Resource.h"
#include "resources/ImageResource.h"
#include "resources/FontResource.h"
#include "resources/AudioClipResource.h"

///Concrete class for imagery

namespace SolidityEngine{

class Resources
{
    static int PathMax;
    static std::string Root;
    static std::set<Resource_ptr> ResourcePool;

    public:
        Resources();
        virtual ~Resources();

        template <typename T> static T* GetResource(int ID){
            std::set<Resource_ptr>::iterator iter=ResourcePool.begin();
            while(iter!=ResourcePool.end()){
                T* test_cast=dynamic_cast<T*>(iter->get());
                if(test_cast){
                    if((*iter)->ID()==ID)
                    {
                        (*iter)->Load();
                        return (test_cast);
                    }
                }
                iter++;
            }
            return NULL;
        };

        template <typename T> static T* GetResource(const std::string& name){
            std::set<Resource_ptr>::iterator iter=ResourcePool.begin();
            while(iter!=ResourcePool.end()){
                T* test_cast=dynamic_cast<T*>(iter->get());
                if(test_cast){
                    if((*iter)->Name()==name)
                    {
                        (*iter)->Load();
                        return (test_cast);
                    }
                }
                iter++;
            }
            return NULL;
        };

        //Although this returns a list of resource references, they aren't loaded
        template <typename T> static std::list<int> GetResourceIDs(){
            std::list<int> outlist=std::list<int>();
            std::set<Resource_ptr>::iterator iter=ResourcePool.begin();
            while(iter!=ResourcePool.end()){
                T* test_cast=dynamic_cast<T*>(iter->get());
                if(test_cast){\
                    outlist.push_back((*iter)->ID());
                }
                iter++;
            }
            return outlist;
        }

        //Although this retrn a list of resource references, they aren't loaded
        template <typename T> static std::list<std::string> GetResourceNames(){
            std::list<std::string> outlist=std::list<std::string>();
            std::set<Resource_ptr>::iterator iter=ResourcePool.begin();
            while(iter!=ResourcePool.end()){
                T* test_cast=dynamic_cast<T*>(iter->get());
                if(test_cast){
                    outlist.push_back((*iter)->Name());
                }
                iter++;
            }
            return outlist;
        }

        static void PutBackResource(const std::string& name);
        static void PutBackResource(int ID);


        //Scan the resources folders, and populate the map
        static void Scan();

        static void Initialize();

    protected:
    private:
};
};
#endif // RESOURCES_H
